package com.lee.rabbitmq;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-12-25
 * Time: 17:25
 */


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 生产消息
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        // 1.创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();

        // 2.设置参数
        factory.setHost("47.236.41.211");
        factory.setPort(5672);
        factory.setVirtualHost("forum");
        factory.setUsername("ITforum");
        factory.setPassword("ihatehacker555");
        // 3.创建连接
        // 4.创建channel
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        // 5.创建队列
        // String queue,
        // boolean durable,
        // boolean exclusive,
        // boolean autoDelete,
        // Map<String, Object> arguments
        channel.queueDeclare("hello.Queue", true, false, false, null);

        // 6.使用默认交换机“”（direct）发送消息（“”匿名交换机忽略绑定关系）
        // String exchange,
        // String routingKey,
        // BasicProperties props,
        // byte[] body
        for (int i = 1; i <= 10; i++) {
            channel.basicPublish("", "hello.Queue", null, ("hello mq!"+i).getBytes());
        }

        Thread.sleep(3000);

        channel.close();
        connection.close();

    }
}
